/*
 * Copyright 2012-2020 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * @author lzhoumail@126.com/zhouli
 * Git http://git.oschina.net/zhou666/spring-cloud-7simple
 */
package cloud.simple.service;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import cloud.simple.model.User;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.client.ClientFactory;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;

@Service
public class UserService{
	@Autowired
    private RestTemplate restTemplate;
    //@Autowired
    //private RestTemplate loadBalanced;
    
    @Autowired
    private EurekaClient discoveryClient;
    
	final String SERVICE_NAME="cloud-simple-service";
	//1.使用原始的Eureka DiscoveryClient 来找到服务实例的主机并且用Spring的RestTemplate来发起调用。
	public String serviceUrl() {
	    InstanceInfo instance = discoveryClient.getNextServerFromEureka(SERVICE_NAME, false);
	    return instance.getHomePageUrl();
	}
	//2.使用Ribbon，一个使用Eureka来找到服务实例的客户端负载均衡解决方案
	public URI serviceUrl2() {
		// get LoadBalancer instance from configuration, properties file
		DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer(SERVICE_NAME);
		// show all servers in the list
		List<Server> list = lb.getServerList(false);
		Iterator<Server> it = list.iterator();
		while (it.hasNext()) {
			Server server = it.next();
			System.out.println("application service host:" + server.getHost() + ";port=" + server.getPort());
		}
		// use RandomRule 's RandomRule algorithm to get a random server from lb 's server list
		/*
		RandomRule randomRule = new RandomRule();
		Server randomAlgorithmServer = randomRule.choose(lb, null);
		System.out.println("random algorithm server host:" + randomAlgorithmServer.getHost() + ";port:" + randomAlgorithmServer.getPort());
		*/
		RoundRobinRule rrr=new RoundRobinRule();
		Server server =rrr.choose(lb, null);
		System.out.println("RoundRobin algorithm server host:" + server.getHost() + ";port:" + server.getPort());
		URI serverURL = URI.create(String.format("http://%s:%s", server.getHost(), server.getPort()));
		return serverURL;
	}
	 
	 @HystrixCommand(fallbackMethod = "fallbackSearchAll")
	 public List<User> searchAll() {
		 System.out.println("开始调用服务端....");
		 System.out.println(serviceUrl());
		 URI uri =serviceUrl2();
		 String reqURL=uri.toString() + "/user";
		 //ResponseEntity<String> resultStr = restTemplate.getForEntity(reqURL, String.class);
		 //System.out.println(resultStr.getBody().toString());
		 //return (T) JSON.parseObject(response.getBody(), clazz);

		 List<User> lss =restTemplate.getForObject(reqURL, List.class);
		 System.out.println("ddddddd==="+lss.toString());
		 //loadBalanced.getForObject("http://"+SERVICE_NAME+"/user", List.class);
		 return lss;
	 }	 
	 private List<User> fallbackSearchAll() {
		 System.out.println("HystrixCommand fallbackMethod handle!");
		 List<User> ls = new ArrayList<User>();
		 User user = new User();
		 user.setUsername("TestHystrixCommand");
		 ls.add(user);
		 return ls;
	 }
	
}
